﻿function ST_SignedTwipsMeasure() {
}
ST_SignedTwipsMeasure.IsValid = function (value) {
    return value.toString().match(/-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)|-?[0-9]+/);
}
function ST_TwipsMeasure() {
}
ST_TwipsMeasure.IsValid = function (value) {
    return value.toString().match(/[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)|/) || (value > 0 && value <= 18446744073709551615);
}
WML.Render = function(wml,div, documentUrl) {
    wml._ParentDiv = div;
    wml._DocumentUrl = documentUrl;

    setTimeout(function () { WML.LayoutTimer(wml); }, this._TimerInterval);
    setTimeout(function () { WML.CursorBlink(wml); }, this._CursorTimerInterval);

    if (wml.ShowTopBar()) {
        WML.CreateWMLTopBarDiv(wml);
    }
    if (wml.ShowMenu()) {
        WML.CreateWMLMenuDiv(wml);
    }
    WML.CreateWMLVerticalScrollBar(div);
    WML.CreateWMLStatusDiv(div);
    WML.RegisterEvents(div);
    WML.RenderWMLShellInitially(this);
    if (documentUrl == null) {
        WML.PrepareWMLForTyping(this);
    }
    else {
        WML.RenderWMLDocument(this);
    }
}

WML.CreateWMLVerticalScrollBar = function (div) {
    
}

WML.LayoutTimer = function(wml) {
    var sizeChanged = false;
    var parentWidth = wml.GetWMLDivWidth();
    if (parentWidth != wml.ParentWidth()) {
        sizeChanged = true;
    }
    var parentHeight = wml.GetWMLDivHeight();
    if (parentHeight != wml.ParentHeight()) {
        sizeChanged = true;
    }
    if (sizeChanged) {
        wml.ParentWidth(parentWidth);
        wml.ParentHeight(parentHeight);
        $(wml.Div()).width(wml.GetWMLDivWidth());
        $(wml.Div()).height(wml.GetWMLDivHeight());
        $("#WMLMenuDiv" + wml.UniqueId()).width(wml.GetWMLDivWidth());
        $("#WMLRibbonDiv" + wml.UniqueId()).width(wml.GetWMLDivWidth());
        wml.RenderDocumentBody();
    }
    if (wml.CursorBlink == 0) {

    }
    else {

    }
    setTimeout(function () { return WMLTimer(wml); }, wml.TimerInterval);
}
WML.RegisterEvents = function (wmlDiv) {
    $(wmlDiv).focus(function () {
    });
    $(wmlDiv).blur(function () {
    });
}

WML.RenderRibbon = function (wml) {
    if (wml.ShowMenu()) {
        $(wml._Div).append("<div id='WMLRibbonDiv" + wml._UniqueId + "' style='position:absolute;top:" + wml._TopBarHeight + "px;width:" + WML.GetWMLDivWidth(wml) + "px;height:" + wml._MenuHeight + "px;background:#CED312;overflow:hidden;margin:none;padding:none;'/>");
    }
}
WML.RenderRulers = function (section, top, left, wml) {
    if (wml.ShowRulers()) {
        var width = section.PgSz().W() / 1440 * this.DPI();
        var height = section.PgSz().H() / 1440 * this.DPI();
        $(div).append("<div id='WMLTopRuler" + wml._UniqueId + "' style='position:absolute;top:" + (wml._TopBarHeight + wml._MenuHeight + top) + "px;left:" + left + "px;width:" + width + "px;height:20px;background:#ABABAB;overflow:hidden;margin:none;padding:none;'/>");
    }
}

WML.CreateWMLMenuDiv = function (wml) {
    var div = wml.ParentDiv();
    var divWidth = $(div).width();
    $(wml.ParentDiv()).append("<div id='WMLMenuDiv' style='position:absolute;top:" + (wml._TopBarHeight + wml._TopBarBorderThickness) + "px;width:" + divWidth + "px;height:" + wml._MenuHeight + "px;background:#F0F1F4;overflow:hidden;margin:none;padding:none;border-bottom:" + wml._MenuBorderThickness + "px solid black;'/>");
}

WML.CreateWMLTopBarDiv = function (wml) {
    var div = wml.ParentDiv();
    var divWidth = $(div).width();
    $(wml.ParentDiv()).append("<div id='WMLTopBarDiv' style='position:absolute;width:" + divWidth + "px;height:" + wml._TopBarHeight + "px;background:#F0F1F4;overflow:hidden;margin:none;padding:none;border-bottom:" + wml._TopBarBorderThickness + "px solid black;'/>");
}

WML.RenderWMLShellInitially = function (wml) {
    var wmlWidth = WML.GetWMLDivWidth(wml);
    var wmlHeight = WML.GetWMLDivHeight(wml);

    $(wml._Div).css('background', '#CCD0D5');
    $(wml._Div).width(wmlWidth);
    $(wml._Div).height(wmlHeight);
    $(wml._Div).append("<div id='WMLMenuDiv" + wml._UniqueId + "' style='position:absolute;width:" + wmlWidth + "px;height:" + wml._TopBarHeight + "px;background:#CED3FF;overflow:hidden;margin:none;padding:none;'/>");
    WML.RenderRibbon(wml);

    this.RenderDocumentBody = function () {
        var documentWidth = this.GetWMLDivWidth();
        var documentBodyDiv = $("<div id='WMLDocumentDiv1" + _UniqueId + "' style='position:absolute;top:" + (this.MenuHeight + (this.ShowMenu() ? this.RibbonHeight : 0)) + "px;width:" + documentWidth + "px;height:" + (this.GetWMLDivHeight() - this.MenuHeight - this.RibbonHeight) + "px;background:#AAFFFF;overflow:hidden;margin:none;padding:none;'/>");
        $(div).append(documentBodyDiv);
        var measuringSpan = $("<span id='WMLDocumentMeasuringSpan1" + _UniqueId + "' style='display:auto;position:absolute;top:0px;left:0px;margin:none;padding:none;'/>");
        $(documentBodyDiv).append(measuringSpan);
        if (this.ShowRulers()) {
            var firstSection = _Document.Body().SectPr();
            var ps = _Document.Body().P("all");
            for (var i = 0; i < ps.length; i++) {
                if (ps[i].pPr != null) {
                    var section = ps[i].pPr().sectPr();
                    if (section != null) {
                        firstSection = section;
                    }
                }
            }
            var pageWidth = firstSection.PgSz().W() / 1440 * this.DPI() * this.Settings.Zoom() / 100;
            var pageHeight = firstSection.PgSz().H() / 1440 * this.DPI() * this.Settings.Zoom() / 100;
            var top = (this.ShowRulers() ? this.RulerHeight : 0) + this.SpaceAfterRibbon;
            var left = (this.ShowRulers() ? this.RulerHeight : 0) + (documentWidth - pageWidth) / 2;
            var pageDiv = $("<div id='WMLPageDiv' style='position:absolute;top:" + top + "px;left:" + left + "px;width:" + pageWidth + "px;height:" + pageHeight + "px;background:#FFFFFF;overflow:hidden;margin:none;padding:none;'/>");
            $(documentBodyDiv).append(pageDiv);
            pageDiv.append(this.CursorDiv);

            this.CursorDiv.css('top', 0);
            this.CursorDiv.css('left', 0);

            var page = new WML_CT_Page();
            this.Pages("add", page);
            page.Div = pageDiv;
            this.Selection.StartingPage = page;
            var cursorTop = top + firstSection.PgMar().Top();
            var cursorLeft = left + firstSection.PgMar().Left();

            this.Selection.StartingLocation = 0;
            this.Selection.EndingLocation = 0;
            this.Selection.StartingPage = page;
            this.Selection.EndingPage = page;

            //for (var i = 0; i < ps.length; i++) {
            //    var children = ps[i].Children("all");
            //    for (var j = 0; j < children.length; j++) {
            //        var child = children[j];
            //        if (child.Classname() == "WML_CT_R") {
            //            var span = $("<Span style='position:absolute;top:" + top + "px;left:" + left + "px;width:" + pageWidth + "px;height:" + pageHeight + "px;background:#123456;overflow:hidden;margin:none;padding:none;'/>");
            //        }
            //    }
            //}
            documentBodyDiv.append(pageDiv);
        }
    }
    $(document.body).height(window.innerHeight);
    var wml = this;
    
}

WML.PrepareWMLForTyping = function (wml) {
    var rsidR = new Date().getMilliseconds().toString(16);
    _Document = new WML_CT_Document();
    var body = new WML_CT_Body();
    _Document.Body(body);
    var p = new WML_CT_P();
    body.P("add", p);
    p.RsidR = rsidR;
    p.RsidRDefault = rsidR;
    var bookmarkStart = new WML_CT_Bookmark();
    bookmarkStart.Id("0");
    bookmarkStart.Name("_GoBack");
    p.Children("add", bookmarkStart);
    var bookmarkEnd = new WML_CT_MarkupRange();
    bookmarkEnd.Id("0");
    p.Children("add", bookmarkEnd);
    var r = new WML_CT_R();
    r.Spans.push(document.createElement('span'));
    wml.Selection.StartingRun = r;
    wml.Selection.StartingParagraph = p;
    p.Children("add", r);
    r.t = "Hello World";
    var sectPr = new WML_CT_SectPr();
    body.SectPr(sectPr);
    sectPr.RsidR(rsidR);
    var pgSz = new WML_CT_PgSz();
    sectPr.PgSz(pgSz);
    pgSz.W(12240);
    pgSz.H(15840);
    var pgMar = new WML_CT_PageMar();
    sectPr.PgMar(pgMar);
    pgMar.Top(1440);
    pgMar.Right(1440);
    pgMar.Bottom(1440);
    pgMar.Left(1440);
    pgMar.Header(720);
    pgMar.Footer(720);
    pgMar.Gutter(0);
    var cols = new WML_CT_Columns();
    sectPr.Cols(cols);
    cols.Space(720);
    var docGrid = new WML_CT_DocGrid();
    sectPr.DocGrid(docGrid);
    docGrid.LinePitch(360);    
    wml.RenderDocumentBody();
}